package com.google.code.task;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

public class AsyncerDecorator extends AbstractFluentTask {
  @SuppressWarnings("unused")
  private static final Log LOG = LogFactory.getLog(AsyncerDecorator.class);
  private static AsyncerManager manager = null;
  private Task task = null;
  private TaskEvent event = null;
  
  public AsyncerDecorator(Task task) {
    this.task = task;
  }

  public TaskResultType execute() {
    manager.delegate(task, event, TaskContext.getLocalMap());
    return TaskResultType.DELEGATION;
  }

  public AsyncerDecorator event(TaskEvent event) {
    this.event = event;
    return this;
  }
  
  public static void startup() {
    if (manager != null) {
      return;
    }
    manager = new AsyncerManager();
    manager.init();    
  }
  
  public static void shutdown() {
    if (manager == null) {
      return;
    }
    manager.shutdown();
    manager = null;
  }

  Task getTask() {
    return task;
  }

  TaskEvent getEvent() {
    return event;
  }
  
  void setAyncerManager(AsyncerManager asyncerManager) {
    manager = asyncerManager;
  }
}